home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 15 / CU Amiga Magazine's Super CD-ROM 15 (1997)(EMAP Images)(GB)[!][issue 1997-10].iso / CUCD / Utilities / Post / Source / SubTask.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-03  |  11.4 KB  |  444 lines

  1. #include "PostPre.h"
  2. #include "Global.h"
  3.  
  4. /* The render task */
  5.  
  6. int    numofpages, page_counter, bandnumber = 0, totalbands;
  7. BOOL    createerror;
  8. char    file[256];
  9. BPTR    outfh;
  10.  
  11. void __saveds SubTask(void)
  12. {
  13.     BPTR        oldlock, newlock;
  14.     struct    PSmessage *msg;
  15.     int        action, ende=FALSE;
  16.     char        dummybuf[256], *status, *file2;
  17.     BOOL        pause_bak, error=TRUE;
  18.  
  19.     insertbreak();
  20.     SetExcept(~0, SIGBREAKF_CTRL_C);
  21.     breakset = TRUE;
  22.     insertftrap();
  23.     ftrapset = TRUE;
  24.  
  25.     pause_signal = AllocSignal(-1);
  26.     if(pause_signal == -1)
  27.     {
  28.         rendertask = NULL;
  29.         return;
  30.     }
  31.     renderport = CreatePort(NULL, 0);
  32.     Signal(maintask, 1L << create_signal);
  33.     if (renderport == NULL)
  34.     {
  35.         FreeSignal(pause_signal);
  36.         rendertask = NULL;
  37.         return;
  38.     }
  39.  
  40.     errfh=Output();
  41.  
  42.     while ( !ende )
  43.     {
  44.         WaitPort(renderport);
  45.         while(msg = (struct PSmessage *) GetMsg(renderport))
  46.         {
  47.             action = msg->action;
  48.             if (msg->file) strcpy(file, FilePart(msg->file));
  49.             numofpages = msg->numofpages;
  50.             running = TRUE;
  51.             retcode = 0;
  52.             errstr = NULL;
  53.             switch (action)
  54.             {
  55.                 case PSACTCREATE:
  56.                     if(arec) deleteactivation();
  57.                     if(argcon)
  58.                     {
  59.                         error=openconwin();
  60.                     }
  61.                     outfh=confh ? confh : errfh;
  62.                     if(error == FALSE || createactivation() == FALSE)
  63.                     {
  64.                         do
  65.                         {
  66.                             ReplyMsg((struct Message *) msg);
  67.                         } while(msg = (struct PSmessage *) GetMsg(renderport));
  68.                         createerror = TRUE;
  69.                         deleteactivation();
  70.                     }
  71.                     else
  72.                     {
  73.                         createerror = FALSE;
  74.                     }
  75.                     Signal(maintask, 1L << create_signal);
  76.                     totalbands = parm.page.yheight / parm.page.ysize;
  77.                     if(parm.page.yheight % parm.page.ysize) totalbands++;
  78.                     break;
  79.  
  80.                 case PSACTCLEAR:
  81.                     PSdeleteact(arec);
  82.                     if(confh)
  83.                     {
  84.                         Close(confh);
  85.                         confh = NULL;
  86.                     }
  87.                     openconwin();
  88.                     parm.infh    = confh;
  89.                     parm.outfh    = confh;
  90.                     parm.errfh    = confh;
  91.                     arec = PScreateact(&parm);
  92.                     if (arec == 0 || (unsigned) arec <= errmax)
  93.                     {
  94.                         okmsg("error during initiation in PScreateact");
  95.                         do
  96.                         {
  97.                             ReplyMsg((struct Message *) msg);
  98.                         } while(msg = (struct PSmessage *) GetMsg(renderport));
  99.                         ende = TRUE;
  100.                         createerror = TRUE;
  101.                     }
  102.                     createerror = FALSE;
  103.                     Signal(maintask, 1L << create_signal);
  104.                     break;
  105.  
  106.                 case PSACTAREXX:
  107.                     if(argwindow)
  108.                     {
  109.                         sprintf(windowtitle,
  110.                                 "Post.%d (Running, Page --- of ---, Interpreting ARexx-Command)",
  111.                                 post_count);
  112.                         if(OutputWnd) SetWindowTitles(OutputWnd, windowtitle, windowtitle);
  113.                     }
  114.                     arexx = TRUE;
  115.                     retcode = PSintstring(arec, msg->directory, -1, PSFLAGSTRING);
  116.                     arexx = FALSE;
  117.                     break;
  118.  
  119.                 case PSACTSTARTUP:
  120.                 case PSACTFILE:
  121.                 case PSACTRUN:
  122.                 case PSACTFONT:
  123.                     if(action == PSACTSTARTUP)
  124.                     {
  125.                         pause_bak = pause;
  126.                         pause = FALSE;
  127.                         status = titlestart;
  128.                         startup = TRUE;
  129.                     }
  130.                     else
  131.                     {
  132.                         status = titlerunning;
  133.                         startup = FALSE;
  134.                     }
  135.                     if(msg->directory)
  136.                     {
  137.                         newlock = Lock(msg->directory, ACCESS_READ);
  138.                         if(newlock)
  139.                         {
  140.                             oldlock = CurrentDir(newlock);
  141.                             if(oldlock) UnLock(oldlock);
  142.                         }
  143.                     }
  144.                     if((argprint || argiff) && 
  145.                         (Options.PostOpts.bandrendering && !bandrendering) &&
  146.                         (action != PSACTSTARTUP) &&
  147.                         (Options.PostOpts.bandsize < Options.PostOpts.height))
  148.                             FPrintf(outfh, "file '%s' not conforming for band-rendering!\n", (ULONG) file);
  149.                     if(argwindow)
  150.                     {
  151.                         if(numofpages) sprintf(windowtitle, "Post.%d (%s, Page %3d of %3d, %s)", post_count, status, 1, numofpages, file);
  152.                         else  sprintf(windowtitle, "Post.%d (%s, Page --- of ---, %s)", post_count, status, file);
  153.                         if(OutputWnd) SetWindowTitles(OutputWnd, windowtitle, windowtitle);
  154.                     }
  155.                     page_counter = msg->from;
  156.                     iffseq = page_counter;
  157.                     if(msg->numofpages)
  158.                     {
  159.                         if (argverbose && !argwindow)
  160.                         {
  161.                             FPrintf(outfh, "interpreting prologue ... ");
  162.                             Flush(outfh);
  163.                         }
  164.                         sprintf(dummybuf, "%sPrologue%d.ps", Options.temppath, post_count);
  165.                         if (retcode = PSintstring(arec, dummybuf, -1, PSFLAGFILE|PSFLAGERASE))
  166.                         {
  167.                             break;
  168.                         }
  169.                         if (argverbose && !argwindow) FPrintf(outfh, "done.\n");
  170.                         if(argprint || argiff)
  171.                         {
  172.                             for (page_counter = msg->from; page_counter <= msg->to; page_counter++)
  173.                             {
  174.                                 bandnumber = 0;
  175.                                 if(argverbose)
  176.                                 {
  177.                                     FPrintf(outfh, "working on page %ld of file '%s', %ld page(s) left:\n", page_counter, (ULONG) file, msg->to - page_counter);
  178.                                     FPrintf(outfh, " rendering band %ld of %ld ... ", 1, totalbands);
  179.                                     Flush(outfh);
  180.                                 }
  181.                                 sprintf(dummybuf, "currentband 1 sub{setband (%sPage%04d_%d.ps) run} for 0 setband", Options.temppath, page_counter, post_count);
  182.                                 if(retcode = PSintstring(arec, dummybuf, -1, PSFLAGSTRING|PSFLAGERASE))
  183.                                 {
  184.                                     break;
  185.                                 }
  186.                             }
  187.                             if (argverbose)
  188.                             {
  189.                                 FPrintf(outfh, "interpreting trailer ... ");
  190.                                 Flush(outfh);
  191.                             }
  192.                             sprintf(dummybuf, "%sTrailer%d.ps", Options.temppath, post_count);
  193.                             retcode = PSintstring(arec, dummybuf, -1, PSFLAGFILE|PSFLAGERASE);
  194.                             if (argverbose) FPrintf(outfh, "done.\n");
  195.                         }
  196.                         else
  197.                         {
  198.                             sprintf(dummybuf, "%sPage%04d_%d.ps", Options.temppath, page_counter, post_count);
  199.                             retcode = PSintstring(arec, dummybuf, -1, PSFLAGFILE|PSFLAGERASE);
  200.                         }
  201.                     }
  202.                     else
  203.                     {
  204.                         page_counter = 1;
  205.                         if(argverbose && OutputWnd == NULL)
  206.                         {
  207.                             if(action == PSACTSTARTUP)
  208.                             {
  209.                                 FPrintf(outfh, "running startup file '%s' ... ", (ULONG) file);
  210.                                 Flush(outfh);
  211.                             }
  212.                             else
  213.                             {
  214.                                 FPrintf(outfh, "rendering page %ld of file '%s' ... ", page_counter, (ULONG) file);
  215.                                 Flush(outfh);
  216.                             }
  217.                         }
  218. /* Versions of HWGPost.library >= 22.26 (Release Beta 6) create a separate    *
  219.  * process. Therefore it is not possible, to change the current directory     *
  220.  * and the full path has to be specified.                                                */
  221.                         if(PSbase->lib_Version == 22)
  222.                         {
  223.                             if(PSbase->lib_Revision >= 26) file2=msg->file;
  224.                             else file2=file;
  225.                         }
  226.                         else if (PSbase->lib_Version > 22) file2=msg->file;
  227.                         else file2=file;
  228.                         retcode = PSintstring(arec, file2, -1, action == PSACTRUN ?  PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE|PSFLAGSAVE : PSFLAGFILE|PSFLAGCLEAR|PSFLAGERASE);
  229.                         if(argverbose && OutputWnd == NULL)
  230.                         {
  231.                             if (action == PSACTSTARTUP) FPrintf(outfh, "done.\n");
  232.                             else if (!ioerror) FPrintf(outfh, "no more pages!\n");
  233.                         }
  234.                     }
  235.                     if(action == PSACTSTARTUP)
  236.                     {
  237.                         pause = pause_bak;
  238.                         startup = FALSE;
  239.                     }
  240.                     break;
  241.  
  242.                 case PSACTINTER:
  243.                     interactive = TRUE;
  244.                     if(argwindow)
  245.                     {
  246.                         sprintf(windowtitle, "Post.%d (Running, Page --- of ---, Interactive)", post_count);
  247.                         if(OutputWnd) SetWindowTitles(OutputWnd, windowtitle, windowtitle);
  248.                     }
  249.                     retcode = PSintstring(arec, "%stdin", -1, PSFLAGFILE | PSFLAGCLEAR | PSFLAGERASE|PSFLAGSAVE|PSFLAGINTER);
  250.                     interactive = FALSE;
  251.                     break;
  252.  
  253.                 case PSACTCLOSE:
  254.                     do
  255.                     {
  256.                         ReplyMsg((struct Message *) msg);
  257.                     } while(msg = (struct PSmessage *) GetMsg(renderport));
  258.                     ende = TRUE;
  259.                     break;
  260.  
  261.                 case PSACTWAIT:
  262.                     Signal(maintask, 1L << create_signal);
  263.                     break;
  264.  
  265.                 case PSACTPAUSE:
  266.                     if(numofpages)
  267.                     {
  268.                         page_counter = msg->from;
  269.                         if (page_counter <= numofpages)
  270.                         {
  271.                             if(argwindow)
  272.                             {
  273.                                 sprintf(windowtitle, "Post.%d (%s, Page %3d of %3d, %s)", post_count, titlerunning, page_counter, numofpages, file);
  274.                                 if(OutputWnd) SetWindowTitles(OutputWnd, windowtitle, windowtitle);
  275.                             }
  276.                             sprintf(dummybuf, "%spage%04d_%d.ps", Options.temppath, page_counter, post_count);
  277.                             retcode = PSintstring(arec, dummybuf, -1, PSFLAGFILE|PSFLAGERASE);
  278.                         }
  279.                         else
  280.                         {
  281.                             sprintf(dummybuf, "%sTrailer%d.ps", Options.temppath, post_count);
  282.                             retcode = PSintstring(arec, dummybuf, -1, PSFLAGFILE|PSFLAGERASE);
  283.                         }
  284.                     }
  285.                     break;
  286.  
  287.                 default:
  288.                     break;
  289.             }
  290.             running = FALSE;
  291.             if(argwindow)
  292.             {
  293.                 sprintf(windowtitle, "Post.%d (Waiting, Page --- of ---, no file loaded)",    post_count);
  294.                 if(OutputWnd) SetWindowTitles(OutputWnd, windowtitle, windowtitle);
  295.             }
  296.  
  297.             if(retcode) errstr = PSerrstr(arec, retcode);
  298.             if(msg) ReplyMsg((struct Message *) msg);
  299.         }
  300.     }
  301.     deleteactivation();
  302.     Signal(maintask, 1L << create_signal);
  303.     FreeSignal(pause_signal);
  304.     DeletePort(renderport);
  305.     if (breakset)
  306.     {
  307.         SetExcept(0, SIGBREAKF_CTRL_C);
  308.         deletebreak();
  309.         breakset = FALSE;
  310.     }
  311.     if (ftrapset)
  312.     {
  313.         deleteftrap();
  314.         ftrapset = FALSE;
  315.     }
  316.  
  317.     renderport = NULL;
  318.     rendertask = NULL;
  319. }
  320.  
  321.  
  322. void    __regargs blit(int, int);
  323.  
  324. void    __regargs blit(int y1, int y2)
  325. {
  326.     if(AttemptLockLayerRom(OutputWnd->WLayer))
  327.     {
  328.         BltBitMapRastPort(&bitmap, winxpos, y1, OutputWnd->RPort,
  329.             winxbase, winybase + y1 - winypos, winxsize, y2 - y1, 0xC0);
  330.         WaitBlit();
  331.         UnlockLayerRom(OutputWnd->WLayer);
  332.     }
  333. }
  334.  
  335. /* Flush the page to the screen */
  336.  
  337. void __saveds __regargs flushpage(int y1, int y2)
  338. {
  339.     if (blit_to_window)
  340.     {
  341.         if (y1 < winypos) y1 = winypos;
  342.         if (y2 < winypos) y2 = winypos;
  343.         if (y1 > winypos + winysize) y1 = winypos + winysize;
  344.         if (y2 > winypos + winysize) y2 = winypos + winysize;
  345.         if (y2 > y1) blit(y1, y2);
  346.     }
  347. }
  348.  
  349. /* Copy the page to the output */
  350.  
  351. void __saveds __regargs copypage(int num)
  352. {
  353.     int    i;
  354.     char    *action;
  355.  
  356.     ioerror = 0;
  357.     if (argiff) action = "saving";
  358.     if (argprint) action = "printing";
  359.     if (numofpages == 0) page_counter++;
  360.     bandnumber++;
  361.     if (argverbose && !argwindow)
  362.     {
  363.         if(numofpages)
  364.         {
  365.             FPrintf(outfh, "done.\n  %s band %ld of %ld", (ULONG) action, bandnumber, totalbands);
  366.         }
  367.         else
  368.         {
  369.             FPrintf(outfh, "done.\n %s page %ld", (ULONG) action, page_counter - 1);
  370.         }
  371.         Flush(outfh);
  372.     }
  373.     if (argiff) iffpage(&output_bitmap);
  374.     if (argprint)
  375.     {
  376.         SetTaskPri((struct Task *) rendertask, 0);
  377.         if (bandrendering)
  378.         {
  379.             if(argverbose && !argwindow)
  380.             {
  381.                 FPrintf(outfh, " ... ");
  382.                 Flush(outfh);
  383.             }
  384.             printpage(&output_bitmap);
  385.         }
  386.         else
  387.         {
  388.             for (i=0; i < num; i++)
  389.             {
  390.                 if(argverbose && !argwindow)
  391.                 {
  392.                     FPrintf(outfh, " copy %ld of %ld ... ", i + 1, num);
  393.                     Flush(outfh);
  394.                 }
  395.                 printpage(&output_bitmap);
  396.             }
  397.         }
  398.         SetTaskPri((struct Task *) rendertask, 0);
  399.     }
  400.     if (argverbose && !argwindow && !ioerror)
  401.     {
  402.         if(numofpages)
  403.         {
  404.             if(bandnumber == totalbands)
  405.             {
  406.                 FPrintf(outfh, "done.\n");
  407.             }
  408.             else
  409.             {
  410.                 FPrintf(outfh, "done.\n rendering band %ld of %ld ... ", bandnumber + 1, totalbands);
  411.             }
  412.         }
  413.         else FPrintf(outfh, "done.\nrendering page %ld of file '%s' ... ", page_counter, (ULONG) file);
  414.         Flush(outfh);
  415.     }
  416.     if (argwindow)
  417.     {
  418.         if (pause)
  419.         {
  420.             paused = TRUE;
  421.             if(numofpages) sprintf(windowtitle, "Post.%d (%s, Page %3d of %3d, %s)", post_count, titlepaused, page_counter, numofpages, file);
  422.             else if(interactive)  sprintf(windowtitle, "Post.%d (%s, Page --- of ---, Interactive)", post_count, titlepaused);
  423.             else if(arexx) sprintf(windowtitle, "Post.%d (%s, Page --- of ---, Interpreting ARexx-Command)", post_count, titlepaused);
  424.             else sprintf(windowtitle, "Post.%d (%s, Page --- of ---, %s)", post_count, titlepaused, file);
  425.             if(OutputWnd) SetWindowTitles(OutputWnd, windowtitle, windowtitle);
  426.             if(blit_to_window)
  427.             {
  428.                     blit(winypos, winysize + winypos);
  429.             }
  430.             Wait(1L << pause_signal);
  431.             if(argwindow && interactive)
  432.             {
  433.                 sprintf(windowtitle, "Post.%d (Running, Page --- of ---, Interactive)", post_count);
  434.                 if(OutputWnd) SetWindowTitles(OutputWnd, windowtitle, windowtitle);
  435.             }
  436.             paused = FALSE;
  437.         }
  438.     }
  439.     if (ioerror && ioerror != errinterrupt)
  440.     {
  441.         PSerror(arec, ioerror);
  442.     }
  443. }
  444.